﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Itfort;
using Itfort.Web;
using Users;
using WanPiJie.AccountSite;
using WanPiJie.AccountSite.Models;

namespace WanPiJie.AccountSite.Controllers
{
	[RequiredLoginActionFilter]
	public class AccountInfoController : BaseController
	{
		//
		// GET: /AccountInfo/
		public ActionResult Index()
		{
			return RedirectToAction("Basic");
		}

		public ActionResult Edit()
		{
			return View();
		}

		[HttpPost]
		public ActionResult Edit(string nickname)
		{
			return View();
		}

		public ActionResult Basic()
		{
			var user = UserWeb.GetOnlineUser();
			if (String.IsNullOrWhiteSpace(user.Email) || String.IsNullOrWhiteSpace(user.Password))
			{
				return RedirectToAction("InitFirst");
			}

			PageResult<Users.User> result = new PageResult<User>();
			result.Entiy = user;
			return View(result);
		}

		[HttpPost]
		public ActionResult Basic(string nickname)
		{
			PageResult<Users.User> result = new PageResult<User>();
			ErrorCode errorCode;
			if (UsersSf.UserService.CheckNickname(nickname, this.User.Id, out errorCode))
			{
				UsersSf.UserService.UpdateNickname(this.User.Id, nickname);
				result.Success();
			}
			else
			{
				result.Failure(errorCode.Description);
			}
			// 重新获取用户实体
			result.Entiy = UsersSf.UserService.GetUser(this.User.Id);
			return View(result);
		}

		// 设置邮箱
		// GET: /AccountInfo/InitFirst
		public ActionResult InitFirst()
		{
			var user = UserWeb.GetOnlineUser();
			if (!String.IsNullOrWhiteSpace(user.Email))
			{
				// 如果已经设置过邮箱，则跳转到密码设置
				return RedirectToAction("InitSecond");
			}

			return View(new PageResult());
		}

		// 设置邮箱
		// POST: /AccountInfo/InitFirst
		[HttpPost]
		public ActionResult InitFirst(string email, string mobile)
		{
			var user = UserWeb.GetOnlineUser();
			if (!String.IsNullOrWhiteSpace(user.Email))
			{
				// 如果已经设置过邮箱，则跳转到密码设置
				return RedirectToAction("InitSecond");
			}

			PageResult result = new PageResult();
			// 邮箱合法性检查
			if (!MyValidator.Required(email) || !MyValidator.Email(email))
			{
				result.Failure("邮箱不能为空或邮箱格式不对");
				return View(result);
			}
			// 邮箱唯一性检查
			if (UsersSf.UserService.ExistsEmail(email))
			{
				result.Failure("邮箱已经存在");
				return View(result);
			}
			// 设置邮箱
			UsersSf.UserService.UpdateEmail(user.Id, email);

			return RedirectToAction("InitSecond");
		}

		// 设置密码
		// GET: /AccountInfo/InitSecond
		public ActionResult InitSecond()
		{
			var user = UserWeb.GetOnlineUser();

			if (!String.IsNullOrEmpty(user.Password))
			{
				// 如果已经设置过密码，则跳转到基础页面
				return RedirectToAction("Basic");
			}

			if (String.IsNullOrEmpty(user.Email))
			{
				// 如果尚未设置过邮箱，则跳转之第一步
				return RedirectToAction("InitFirst");
			}

			AccountInitModel model = new AccountInitModel();
			model.Email = user.Email;

			return View(model);
		}

		// 设置密码
		// POST: /AccountInfo/InitSecond
		[HttpPost]
		public ActionResult InitSecond(AccountInitModel model)
		{
			var user = UserWeb.GetOnlineUser();

			if (!String.IsNullOrEmpty(user.Password))
			{
				// 如果已经设置过密码，则跳转到基础页面
				return RedirectToAction("Basic");
			}

			if (String.IsNullOrEmpty(user.Email))
			{
				// 如果尚未设置过邮箱，则跳转之第一步
				return RedirectToAction("InitFirst");
			}

			model.Password = model.Password.Trim();
			model.PasswordAgain = model.PasswordAgain.Trim();

			// 验证密码有效性
			if (!MyValidator.Required(model.Password) || !MyValidator.RangeLength(model.Password, 6, 30) || model.Password != model.PasswordAgain)
			{
				model.Email = user.Email;
				return View(model);
			}

			UsersSf.UserService.UpdatePassword(user.Id, model.Password);

			return RedirectToAction("InitComplete");
		}

		public ActionResult InitComplete()
		{
			return View();
		}

		public ActionResult Password()
		{
			return View(new PageResult());
		}

		[HttpPost]
		public ActionResult Password(string password, string newPassword, string newPasswordAgain)
		{
			PageResult result = new PageResult();
			password = password.Trim();
			if (this.ValidatePassword(ref newPassword, ref newPasswordAgain))
			{
				if (UsersSf.UserService.ValidatePassword(password, User.PasswordSalt, User.Password))
				{
					UsersSf.UserService.UpdatePassword(User.Id, newPassword);
					result.Success();
				}
				else
				{
					result.Failure("当前密码不正确");
				}
			}
			else
			{
				result.Failure("新密码设置不符合要求");
			}
			return View(result);
		}

		public ActionResult Email()
		{
			ViewBag.Email = this.User.Email;
			return View(new PageResult());
		}

		[HttpPost]
		public ActionResult Email(string email)
		{
			PageResult result = new PageResult();
			ErrorCode errorCode;
			if (UsersSf.UserService.CheckEmail(email, this.User.Id, out errorCode))
			{
				UsersSf.UserService.UpdateEmail(this.User.Id, email);
				result.Success();
			}
			else
			{
				result.Failure(errorCode.Description);
			}

			ViewBag.Email = email;
			return View(result);
		}

		[NonAction]
		private bool ValidatePassword(ref string password, ref string passwordAgain)
		{
			password = password.Trim();
			passwordAgain = passwordAgain.Trim();

			return MyValidator.Required(password)
				&& MyValidator.RangeLength(password, 6, 30)
				&& password == passwordAgain;
		}
	}
}
